<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="">
              <a-select v-model="flid" placeholder="请选择成果类别">
                <a-select-option v-for="(item, index) in cxjSbList" :key="index" :value="item.fl_bh">{{item.fl_mc}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="">
                            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                            </span>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="text-align:right;">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
                <span slot="action" slot-scope="text, record">
                    <a @click="handleEdit(record)">编辑</a>
                    <a-divider type="vertical" />
                    <a @click="handleDelete(record.id)">删除</a>
                </span>
        <!-- 分配成果 -->
        <span slot="resultsAction" slot-scope="text, record">
                    <span style="margin-right:10px;">{{record.xmsl}}</span>
                    <a>
                        <img @click="handleResult(record)" style="width:14px;height:14px;cursor:pointer;" src="../../assets/img/xuanze.png" alt="">
                    </a>
                </span>
        <!-- 分配专家 -->
        <span slot="expertAction" slot-scope="text, record">
                    <span style="margin-right:10px;">{{record.zjlist}}</span>
                    <a>
                        <img @click="handleExpert(record)" style="width:14px;height:14px;cursor:pointer;" src="../../assets/img/xuanze.png" alt="">
                    </a>
                </span>
      </a-table>
    </div>
    <cxj-fs-fz-modal ref="modalForm" @ok="modalFormOk"></cxj-fs-fz-modal>
    <cxj-jsfs-fp-zj ref="fpZjForm" :expertFlag="expertFlag" :flId="flId" :fsfzid="fsfzid" @addExpert="handleAddExpert"></cxj-jsfs-fp-zj>
    <cxj-jsfs-fp-cg ref="fpCgForm" :resultFlag="resultFlag" :flId="flId" :fsfzid="fsfzid" @addResult="handleAddResult"></cxj-jsfs-fp-cg>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { filterObj } from '@/utils/util';
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CxjFsFzModal from './modules/CxjFsFzModal'
// 分配成果
import CxjJsfsFpCg from './modules/CxjJsfsFpCg'
// 分配专家
import CxjJsfsFpZj from './modules/CxjJsfsFpZj'
import {
  sysCxjFlCxjsbList,
  // 添加专家
  cxjfsFzzjadd,
  // 添加项目
  cxjfsFzxmadd
} from "@/api/api"
import {
  deleteAction,
  getAction
} from "@/api/manage"
export default {
  name: 'CxjFsFzList',
  // mixins:[JeecgListMixin, mixinDevice],
  mixins:[mixinDevice],
  components: {
    CxjFsFzModal,
    CxjJsfsFpCg,
    CxjJsfsFpZj
  },
  data () {
    return {
      description: '复审分组表管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'分组编号',
          align:"center",
          dataIndex: 'fs_fz_bh'
        },
        {
          title:'分组名称',
          align:"center",
          dataIndex: 'fs_fz_mc'
        },
        {
          title:'所在会场',
          align:"center",
          dataIndex: 'fs_fz_hcmc'
        },
        {
          title:'会场地点',
          align:"center",
          dataIndex: 'fs_fz_hcdd'
        },
        {
          title:'备注',
          align:"center",
          dataIndex: 'bz1'
        },
        {
          title:'分组类别',
          align:"center",
          dataIndex: 'fl_id',
          customRender: function(id){
            return id == 11 ? '技术成果': id == 21?'系统成果':id == 31?'标准成果':id==41?'信息化成果':id=='51'?'专利成果':''
          },
        },
        {
          title:'已分配成果',
          align:"center",
          dataIndex: 'resultsAction',
          scopedSlots: { customRender: 'resultsAction' }
        },
        {
          title:'分组专家',
          align:"center",
          dataIndex: 'expertAction',
          scopedSlots: { customRender: 'expertAction' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/cxj/cxjFsFz/cxjfs_fzlist",
        delete: "/cxj/cxjFsFz/delete",
        deleteBatch: "/cxj/cxjFsFz/deleteBatch",
        exportXlsUrl: "/cxj/cxjFsFz/exportXls",
        importExcelUrl: "cxj/cxjFsFz/importExcel",
      },
      dictOptions:{},
      superFieldList:[],

      // 成果类别
      cxjSbList: [],
      // 成果id
      flid: '',
      /* 分页列表参数开始 */
      queryParam: {},
      dictOptions:{},
      superFieldList:[],
      fileStatus: "",
      selectedRowKeys: [],
      selectionRows: [],
      loading: false,
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      dataSource: [],
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      /* 分页列表参数结束 */

      // 成果弹框
      resultFlag: false,
      // 专家弹框
      expertFlag: false,
      flId: "",
      fsfzid: "",
    }
  },
  created() {
    this.$set(this.dictOptions, 'isValid', [{text:'是',value:'Y'},{text:'否',value:'N'}])
    this.getSuperFieldList();
    this.getsysCxjFlCxjsbList();
    this.loadData();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    /* 获取成果类别开始 */
    getsysCxjFlCxjsbList(){
      sysCxjFlCxjsbList(null).then(res=>{
        if (res.success) {
          this.cxjSbList = res.result;
        }
      })
    },
    /* 获取成果类别结束 */
    // 删除分组
    handleDelete(id){
      let that = this;
      this.$confirm({
        title: '确定要删除吗?',
        // content: 'Some descriptions',
        okText: '确定',
        // okType: 'danger',
        cancelText: '取消',
        onOk() {
          // let formData = new FormData();
          // formData.append('id', id);
          let formData = {
            id
          }
          deleteAction(that.url.delete, formData).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
              that.loadData();
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        },
        onCancel() {
          console.log('Cancel');
        },
      });
    },
    // 分配成果
    handleResult(record){
      this.resultFlag = !this.resultFlag;
      this.flId = record.fl_id;
      this.fsfzid = record.id;
    },
    // 分配专家
    handleExpert(record){
      this.expertFlag = !this.expertFlag;
      this.flId = record.fl_id;
      this.fsfzid = record.id;
    },
    // 添加项目
    handleAddResult(data){
      let formData = new FormData();
      formData.append('ids', data.ids);
      formData.append('fsfzid', data.fsfzid);
      cxjfsFzxmadd(formData).then(res=>{
        if (res.success) {
          this.$message.success(res.message);
          this.$refs.fpCgForm.selectedResultlist();
          this.$refs.fpCgForm.waitResultList();
          this.$refs.fpCgForm.selectedRowKeys = [];
          this.loadData();
          // this.resultFlag = !this.resultFlag;
        } else {
          this.$message.success(res.message);
        }
      })
    },
    // 添加专家
    handleAddExpert(data){
      let formData = new FormData();
      formData.append('csfzid', data.fsfzid);
      formData.append('ids', data.ids);
      cxjfsFzzjadd(formData).then(res=>{
        console.log(res)
        if (res.code == 200) {
          this.$message.success(res.message);
          this.$refs.fpZjForm.waitExpertList();
          this.$refs.fpZjForm.selectedExpertList();
          this.$refs.fpZjForm.expertIds = [];
          this.loadData();
        } else {
          this.$message.warning(res.message);
        }

      })
    },










    /* 获取列表固定格式开始 */
    getQueryField() {
      // TODO  字段权限控制
      var str = "id,";
      this.columns.forEach(function (value) {
        str += "," + value.dataIndex;
      });
      return str;
    },
    // 获取分组项目
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      // 分组id
      // this.queryParam.sbnd = this.sbnd;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result;
          if (res.result.total) {
            this.ipagination.total = res.result.total;
          } else {
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false;
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
    },
    modalFormOk() {
      // 新增/修改 成功时，重载列表
      this.loadData();
      //清空列表选中
      this.onClearSelected()
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field;
        this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
      }
      this.ipagination = pagination;
      this.loadData();
    },

    getQueryParams() {
      //获取查询条件
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      this.queryParam.flid = this.flid;
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },
    handleAdd: function () {
      this.$refs.modalForm.add();
      this.$refs.modalForm.title = "新增";
      this.$refs.modalForm.disableSubmit = false;
    },
    handleEdit: function (record) {
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "编辑";
      this.$refs.modalForm.disableSubmit = false;
    },
    searchQuery() {
      this.loadData(1);
    },
    searchReset() {
      this.queryParam = {};
      this.flid = "";
      this.loadData(1);
    },
    /* 获取列表固定格式结束 */




    initDictConfig(){},
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'string',value:'flId',text:'成果类别id',dictCode:''})
      fieldList.push({type:'string',value:'fsFzXh',text:'分组序号',dictCode:''})
      fieldList.push({type:'string',value:'fsFzBh',text:'分组编号',dictCode:''})
      fieldList.push({type:'string',value:'fsFzMc',text:'分组名称',dictCode:''})
      fieldList.push({type:'string',value:'cxjFsHcId',text:'复审会场ID',dictCode:''})
      fieldList.push({type:'string',value:'shijian',text:'年度',dictCode:''})
      fieldList.push({type:'switch',value:'isValid',text:'是否有效'})
      fieldList.push({type:'string',value:'fsFzHcmc',text:'会场名称',dictCode:''})
      fieldList.push({type:'string',value:'fsFzHcdd',text:'会场地点',dictCode:''})
      fieldList.push({type:'string',value:'bz1',text:'备注1',dictCode:''})
      fieldList.push({type:'string',value:'bz2',text:'备注2',dictCode:''})
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>